/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * StandAloneGameClient.java
 *
 * Created on 18-May-2009, 3:51:27 PM
 */

package jogs.client.games;

import org.jdesktop.layout.GroupLayout;
import javax.swing.JPanel;
import java.awt.event.KeyEvent;

/**
 *
 * @author lanctot
 */
public class StandAloneGameClient extends javax.swing.JFrame {

    GameClientPanel mainPanel;
    private static boolean visible = true;


    /** Creates new form StandAloneGameClient */
    public StandAloneGameClient() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  private void initComponents() {

    jSplitPane1 = new javax.swing.JSplitPane();
    jSplitPane2 = new javax.swing.JSplitPane();
    jSplitPane3 = new javax.swing.JSplitPane();
    jScrollPane1 = new javax.swing.JScrollPane();
    commandTextArea = new javax.swing.JTextArea();
    jScrollPane2 = new javax.swing.JScrollPane();
    statusList = new javax.swing.JList();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    jSplitPane1.setDividerLocation(900);

    jSplitPane2.setDividerLocation(625);
    jSplitPane2.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);
    jSplitPane1.setLeftComponent(jSplitPane2);

    jSplitPane3.setDividerLocation(725);
    jSplitPane3.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);

    commandTextArea.setColumns(20);
    commandTextArea.setRows(2);
    commandTextArea.addKeyListener(new java.awt.event.KeyAdapter() {
      public void keyReleased(java.awt.event.KeyEvent evt) {
        commandTextAreaKeyReleased(evt);
      }
    });
    jScrollPane1.setViewportView(commandTextArea);

    jSplitPane3.setRightComponent(jScrollPane1);

    jSplitPane1.setLeftComponent(jSplitPane3);

    statusList.setModel(new javax.swing.AbstractListModel() {
      String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
      public int getSize() { return strings.length; }
      public Object getElementAt(int i) { return strings[i]; }
    });
    jScrollPane2.setViewportView(statusList);

    jSplitPane1.setRightComponent(jScrollPane2);

    org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
      layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
      .add(jSplitPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 1035, Short.MAX_VALUE)
    );
    layout.setVerticalGroup(
      layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
      .add(jSplitPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 788, Short.MAX_VALUE)
    );

    pack();
  }// </editor-fold>//GEN-END:initComponents

    private void commandTextAreaKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_commandTextAreaKeyReleased
      // TODO add your handling code here:
      if (evt.getKeyCode() == KeyEvent.VK_ENTER)
      {
        String command = (commandTextArea.getText()).trim();
        commandTextArea.setText("");

        // do something with it
        mainPanel.handleCommand(command);
      }
    }//GEN-LAST:event_commandTextAreaKeyReleased

    private void loadGame(String game, String[] args)
    {
      mainPanel = GameClient.newGamePanel(game);
      
      org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(mainPanel);
      mainPanel.setLayout(jPanel1Layout);
      jPanel1Layout.setHorizontalGroup(
        jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
        .add(0, 801, Short.MAX_VALUE)
      );
      jPanel1Layout.setVerticalGroup(
        jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
        .add(0, 625, Short.MAX_VALUE)
      );

      jSplitPane3.setLeftComponent(mainPanel);

      mainPanel.startStandAlone(args);

      mainPanel.setVisible(visible);
    }

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {

      if (args.length > 2 && args[2].equals("random"))
        visible = false;

      StandAloneGameClient sagc = new StandAloneGameClient();

      String game = args[0];
      sagc.loadGame(game, args);

      sagc.setVisible(visible);
    }

  // Variables declaration - do not modify//GEN-BEGIN:variables
  private javax.swing.JTextArea commandTextArea;
  private javax.swing.JScrollPane jScrollPane1;
  private javax.swing.JScrollPane jScrollPane2;
  private javax.swing.JSplitPane jSplitPane1;
  private javax.swing.JSplitPane jSplitPane2;
  private javax.swing.JSplitPane jSplitPane3;
  private javax.swing.JList statusList;
  // End of variables declaration//GEN-END:variables

}
